﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template name="lowercase">
    <xsl:param name="src"/>
    <xsl:variable name="lower-case">abcedfghijklmnopqrstuvwxyz</xsl:variable>
    <xsl:variable name="upper-case">ABCEDFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
    <xsl:value-of select="translate($src, $upper-case, $lower-case)"/>
  </xsl:template>  
  <!--Uppercase a word. e.g. from sample to SAMPLE  -->
  <xsl:template name="uppercase">
    <xsl:param name="src"/>
    <xsl:variable name="lower-case">abcedfghijklmnopqrstuvwxyz</xsl:variable>
    <xsl:variable name="upper-case">ABCEDFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
    <xsl:value-of select="translate($src, $lower-case, $upper-case)"/>
  </xsl:template>
  <!--Capitalize a word.e.g. from SAMPLE to sample -->
  <xsl:template name="capitalized">
    <xsl:param name="src"/>
    <xsl:variable name="lower-case">abcedfghijklmnopqrstuvwxyz</xsl:variable>
    <xsl:variable name="upper-case">ABCEDFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
    <xsl:variable name="first-letter">
      <xsl:call-template name="uppercase">
        <xsl:with-param name="src" select="substring($src,1,1)"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="word-left">
      <xsl:call-template name="lowercase">
        <xsl:with-param name="src" select="substring($src,2)"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="concat($first-letter, $word-left)"/>
  </xsl:template>
  <!--capitalize all words in a sentence. e.g. from this IS a SamPle to This is A Sample-->
  <xsl:template name="capitalizedaround">
    <xsl:param name="src"/>
    <xsl:param name="sep"/>
    <xsl:variable name="first-word" select="substring-before($src, $sep)"/>
    <xsl:variable name="words-left" select="substring-after($src, $sep)"/>
    <xsl:variable name="current">
      <xsl:call-template name="capitalized">
        <xsl:with-param name="src" select="$first-word"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="concat($current, string($sep))"/>
    <xsl:if test="string-length($words-left) > 0">
      <xsl:call-template name="capitalizedaround">
        <xsl:with-param name="src" select="$words-left"/>
        <xsl:with-param name="sep" select="$sep"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
